package com.elitel.frame.base.service.impl;

import com.elitel.frame.base.component.exportprojectdata.DataBaseConnInfo;
import com.elitel.frame.base.service.UserInfoService;
import com.elitel.frame.main.dao.UserInfoMapper;
import com.elitel.frame.main.dao.ext.ExtUserInfoMapper;
import com.elitel.frame.main.entity.ext.ExtUserInfo;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoServiceImpl implements UserInfoService {
  @Autowired private UserInfoMapper userInfoMapper;
  @Autowired private ExtUserInfoMapper extUserInfoMapper;
  @Autowired private DataBaseConnInfo dataBaseConnInfo;

  @Override
  public int insertUserInfo(ExtUserInfo extUserInfo) {
    int result = userInfoMapper.insertSelective(extUserInfo);
    insertProjectToUserBatch(extUserInfo);
    return result;
  }

  @Override
  public int updateUserInfo(ExtUserInfo userInfo, boolean updateProject) {
    int result = userInfoMapper.updateByPrimaryKeySelective(userInfo);
    // 是否更新授权项目
    if (updateProject) {
      extUserInfoMapper.deleteProjectByLoginname(userInfo.getLoginname());
      insertProjectToUserBatch(userInfo);
    }
    return result;
  }

  @Override
  public int deleteUserInfo(String loginname) {
    int result = extUserInfoMapper.deleteByLoginname(loginname);
    extUserInfoMapper.deleteProjectByLoginname(loginname);
    return result;
  }

  @Override
  public ExtUserInfo selectByLoginName(String loginName) {
    return extUserInfoMapper.selectByLoginName(loginName);
  }

  /**
   * 获取用户列表
   *
   * @param pr
   * @return
   */
  @Override
  public PageResponseVo<ExtUserInfo> getUserInfoList(PageRequestVo<ExtUserInfo> pr) {
    PageResponseVo<ExtUserInfo> responseVo = new PageResponseVo<ExtUserInfo>();
    pr.setPageNo((pr.getPageNo() - 1) * pr.getPageSize());
    List<ExtUserInfo> userInfoByPagination = extUserInfoMapper.getUserInfoByPagination(pr);
    Long count = extUserInfoMapper.getCountByPagination(pr);
    responseVo.setRows(userInfoByPagination);
    responseVo.setTotal(count.intValue());
    return responseVo;
  }

  /**
   * 根据 userID 获取用户信息
   *
   * @param userId
   * @return
   */
  @Override
  public ExtUserInfo selectByUserId(String userId) {
    ExtUserInfo userInfo = extUserInfoMapper.selectByUserId(userId);
    userInfo.setProject(extUserInfoMapper.selectProjectByLoginname(userInfo.getLoginname()));
    return userInfo;
  }

  @Override
  public ExtUserInfo selectByAuthorizationCode(String authorizationCode) {
    return extUserInfoMapper.selectByAuthorizationCode(authorizationCode);
  }

  @Override
  public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    ExtUserInfo extUserInfo = extUserInfoMapper.selectByLoginName(s);
    if (extUserInfo == null) return null;

    return User.withUsername(extUserInfo.getUsername())
        .password(extUserInfo.getPassword())
        .authorities(extUserInfo.getRole())
        .build();
  }

  /**
   * 批量添加项目对应用户
   *
   * @param extUserInfo
   */
  private void insertProjectToUserBatch(ExtUserInfo extUserInfo) {
    if (extUserInfo.getProject() != null && extUserInfo.getProject().size() > 0) {
      Map<String, Object> map = new HashMap<>();
      map.put("loginname", extUserInfo.getLoginname());
      map.put("list", extUserInfo.getProject());
      if (dataBaseConnInfo.dataBaseType.equals("sqlite")){
        extUserInfoMapper.insertProjectToUserBatchSqlite(map);
      }else {
        extUserInfoMapper.insertProjectToUserBatch(map);
      }
    }
  }
}
